//
//  SearchExpertViewController.m
//  DaJiang
//
//  Created by sally on 16/10/23.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "SearchExpertViewController.h"
#import "ThemeCell.h"
#import "ExpertModel.h"

@interface SearchExpertViewController () <UISearchBarDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *tableData;

@end

@implementation SearchExpertViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  [self setNav];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = [NSMutableArray array];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
  return cell.frame.size.height;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  ThemeCell *cell = (ThemeCell *) [tableView dequeueReusableCellWithIdentifier:@"ThemeCell"];
  if (!cell) {
    cell = [[[NSBundle mainBundle] loadNibNamed:@"ThemeCell" owner:nil options:nil] lastObject];
  }

  cell.cellExpertData = [self.tableData objectAtIndex:indexPath.row];
  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  ExpertModel *expertModel = [self.tableData objectAtIndex:indexPath.row];
  [OperationManager gotoExpertDetailVcWithId:expertModel.expertid expertName:expertModel.user_data.user_name];
}

#pragma mark searchBar delegate
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
  [searchBar resignFirstResponder];//搜索时使搜索框失焦
  NSString *locName = [[NSUserDefaults standardUserDefaults] objectForKey:UD_LAST_SELECT_LOC];
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];

  [ExpertModel searchExpertListWithLocation:locName content:searchBar.text completion:^(NSMutableArray *expertList) {
    if (expertList) {
      [self.tableData removeAllObjects];
      [self.tableData addObjectsFromArray:expertList];
      [self.tableView reloadData];
      if (expertList.count < 1) {
        [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"无搜索结果"];
      }
    } else {
      NSError *error = [ExpertModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
}

#pragma mark - 私有方法
#pragma mark 设置导航栏
- (void)setNav {
  UISearchBar *searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth - 54, NavHeight)];
  searchBar.tintColor = [UIColor whiteColor];
  searchBar.barStyle = UIBarStyleBlack;
  searchBar.delegate = self;
  [searchBar becomeFirstResponder];
  self.navigationItem.titleView = searchBar;
  self.navigationItem.hidesBackButton = YES;
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleDone target:self action:@selector(back)];
  [[CommonMethod sharedCommonMethod] setNavButtonItem:self.navigationItem.rightBarButtonItem type:ButtonItemType_Gray];
}

- (void)back {
  [self.navigationController popViewControllerAnimated:YES];
}

@end
